package com.tencent.miniproject.presenter;

import android.util.Log;

import com.tencent.miniproject.been.HotTag;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.presenter.i.ISearchPresenter;
import com.tencent.miniproject.ui.i.ISearchActivity;
import com.tencent.miniproject.utils.GsonUtil;
import com.tencent.miniproject.utils.OkHttpResultCallback;
import com.tencent.miniproject.utils.OkHttpUtil;

import java.io.UnsupportedEncodingException;

import okhttp3.Call;

/**
 * Created by PC on 2018/5/30.
 */

public class SearchPresenter implements ISearchPresenter {
    private static final String TAG = "SearchPresenter";
    private ISearchActivity mISearchActivity;

    public SearchPresenter(ISearchActivity iSearchActivity) {
        mISearchActivity = iSearchActivity;
    }

    @Override
    public void getHotTag() {
        String url = Constant.BASEURL+"search/getHotTags";
        OkHttpUtil.getInstance().getAsync(url, new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                Log.e(TAG, "onError: " + e.getMessage() );
                mISearchActivity.getHotTagError(e.getMessage());
            }

            @Override
            public void onResponse(byte[] bytes) {
                try {
                    String msg = new String(bytes,"utf-8");
                    Log.e(TAG, "onResponse: " + msg);
                    HotTag hotTag = GsonUtil.gsonToBean(msg,HotTag.class);
                    if (hotTag.getResult().equals("success")){
                        mISearchActivity.getHotTagSuccess(hotTag.getMessage());
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
